const api = require("../../../../ws_micro_shop/utils/api.js");
const app = getApp();

Page({
  data: {
    page_img: {},
    classa:
      (app.tabbarindex === 0 ? false : true) ||
      app.siteInfo.tabbar_type === "2",
    unit: "",
    abnorData: {
      imgSrc: "/common/static/images/abnor-06.png",
      text: "商家还未设置活动规则哟~"
    },
    useTabbar: !(app.tabbarindex === 0 && app.siteInfo.tabbar_type === "1"),
    isHongbaoOpen: true
  },

  onLoad(opts) {
    let options = opts || {};
    let _this = this;
    app.tabbarindex = options.tabbarindex
      ? options.tabbarindex - 0
      : app.tabbarindex;
    let imageLine = api.default.index.substr(
      0,
      api.default.index.indexOf("/index.php")
    );
    app.loadCurrencyUnit().then(res => {
      _this.setData({
        unit: res.unit
      });
    });
    this.setData({
      page_img: {
        bg: imageLine + "/statics/images/fxhb/bg.png",
        close: imageLine + "/statics/images/fxhb/close.png",
        hongbao_bg: imageLine + "/statics/images/fxhb/hongbao_bg.png",
        open_hongbao_btn:
          imageLine + "/statics/images/fxhb/open_hongbao_btn.png"
      },
      opts: options
    });
    app.pageOnLoad(_this);
    wx.showLoading({
      title: "加载中",
      mask: true
    });
    app.ws_micro_shoprequest({
      url: api.fxhb.open,
      success: function(res) {
        wx.hideLoading();
        if (res.code === 0) {
          if (res.data.background_pic && res.data.background_pic !== "") {
            _this.setData({
              "page_img.bg": res.data.background_pic
            });
          }
          if (res.data.hongbao_id) {
            wx.navigateTo({
              url: `/ws_micro_shop_seller/pages/fxhb/detail/detail?id=${res.data.hongbao_id}`
            });
          }
          _this.setData(res.data);
        }
        if (res.code === 1) {
          wx.showModal({
            content: res.msg,
            showCancel: false,
            success: function(res) {
              if (res.confirm) {
                app.util.navigateTo(
                  "/ws_micro_shop/pages/index/index",
                  {},
                  true
                );
              }
            }
          });
        }
      }
    });
  },

  showRule() {
    this.setData({
      showRule: true
    });
  },

  showHistory() {
    app.util.navigateTo("/ws_micro_shop_seller/pages/fxhb/list/list");
  },

  closeRule() {
    this.setData({
      showRule: false
    });
  },

  openHongbao(e) {
    let that = this;
    wx.showLoading({
      title: "抢红包中",
      mask: true
    });
    this.setData({
      isHongbaoOpen: false
    });
    app.ws_micro_shoprequest({
      url: api.fxhb.open_submit,
      method: "POST",
      data: {
        form_id: e.detail.formId
      },
      success: function(res) {
        if (res.code === 0) {
          that.setData({
            isHongbaoOpen: true
          });
          wx.navigateTo({
            url: `/ws_micro_shop_seller/pages/fxhb/detail/detail?id=${res.data.hongbao_id}`
          });
        } else {
          that.setData({
            isHongbaoOpen: true
          });
          wx.hideLoading();
          wx.showModal({
            content: res.msg,
            showCancel: false
          });
        }
      }
    });
  }
});
